<?php

namespace models\DAO;

use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;


/**
 * @SWG\Swagger(
 *     @SWG\Definition(
 *          type="object",
 *          definition="orderSingleData",
 *          required={"errno", "errmsg", "data"},
 *          @SWG\Property(property="errno", type="integer", format="int32", description="编码"),
 *          @SWG\Property(property="errmsg", type="string", format="string", description="错误提示信息"),
 *          @SWG\Property(property="result", type="object", ref="#/definitions/orderSingleObj")
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="orderSingleObj",
 *          @SWG\Property(property="data", type="object", ref="#/definitions/orderSingle")
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="orderListData",
 *          required={"errno", "errmsg", "result"},
 *          @SWG\Property(property="errno", type="integer", format="int32", description="编码"),
 *          @SWG\Property(property="errmsg", type="string", format="string", description="错误提示信息"),
 *          @SWG\Property(property="result", ref="#/definitions/orderListObj"),
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="orderListObj",
 *          @SWG\Property(property="data", type="object", ref="#/definitions/orderData"),
 *          @SWG\Property(property="meta", ref="#/definitions/meta")
 *     ),
 *     @SWG\Definition(
 *          type="array",
 *          definition="orderData",
 *          @SWG\Items(
 *              title="data", ref="#/definitions/orderSingle"
 *          )
 *     ),
 *     @SWG\Definition(
 *          type="object",
 *          definition="orderSingle",
 *          @SWG\Property(property="sn", type="string", format="string",  description="订单编号"),
 *          @SWG\Property(property="fail_sn", type="string", format="string",  description="拼团失败订单号"),
 *          @SWG\Property(property="trade_order_sn", type="string", format="string",  description="1688订单号"),
 *          @SWG\Property(property="type", type="integer", format="int32",  description="订单类型(0为购买,1为兑换,默认0)"),
 *          @SWG\Property(property="product_id", type="string", format="string",  description="产品编号"),
 *          @SWG\Property(property="sku_id", type="string", format="string",  description="sku编号"),
 *          @SWG\Property(property="activity_id", type="integer", format="int32",  description="活动编号"),
 *          @SWG\Property(property="member_id", type="integer", format="int32",  description="下单会员"),
 *          @SWG\Property(property="fail_member_id", type="integer", format="int32",  description="拼团失败会员"),
 *          @SWG\Property(property="address_id", type="integer", format="int32",  description="收货地址"),
 *          @SWG\Property(property="pay_type", type="integer", format="int32",  description="支付通道(0为支付宝,1为微信,2为余额,默认0)"),
 *          @SWG\Property(property="amount", type="string", format="string",  description="价格"),
 *          @SWG\Property(property="fee", type="string", format="string",  description="邮费"),
 *          @SWG\Property(property="stats", type="integer", format="int32",  description="状态(0待支付,1为待发货,2为已发货,3为确认收货,18下单失败,当type为1时,该字段默认值为1)"),
 *          @SWG\Property(property="refund_stats", type="integer", format="int32",  description="退货退款状态(0为不退款不退货,1为发起退款,2为发起退货,3为退款成功,4为退货成功,5为退款失败,6为退货失败（卖家拒绝）,7为退货等待买家发货,8为退货买家已发货,9退货失败（卖家拒收）默认为0,当type为1时,该字段固定值0.当stats为1时方可发起退款,当stats为3时方可发起退货)"),
 *          @SWG\Property(property="rate_stats", type="integer", format="int32",  description="评价状态(0为买家未评价,1为买家已评价,默认0,当stats为3时方可修改为1,买家评价后变为2)"),
 *          @SWG\Property(property="post_fee", type="string", format="string",  description="运费"),
 *          @SWG\Property(property="cost", type="string", format="string",  description="成本"),
 *          @SWG\Property(property="service_fee", type="string", format="string",  description="退款退货服务费"),
 *          @SWG\Property(property="notified_at", type="string", format="string",  description="异步回调时间"),
 *          @SWG\Property(property="refunded_at", type="string", format="string",  description="发起退款时间"),
 *          @SWG\Property(property="returned_at", type="string", format="string",  description="发起退货时间"),
 *          @SWG\Property(property="created_at", type="string", format="string",  description="下单时间"),
 *          @SWG\Property(property="coupon_at", type="string", format="string",  description="开始拼团时间"),
 *          @SWG\Property(property="updated_at", type="string", format="string",  description="更新时间"),
 *     )
 * )
 */
class OrderModel extends BaseModel{
    /** 
     * 订单编号
     */
    public $sn = null;

    /** 
     * 拼团失败订单号
     */
    public $fail_sn = null;

    /** 
     * 1688订单号
     */
    public $trade_order_sn = null;

    /** 
     * 订单类型(0为购买,1为兑换,默认0)
     */
    public $type = null;

    /** 
     * 产品编号
     */
    public $product_id = null;

    /** 
     * sku编号
     */
    public $sku_id = null;

    /** 
     * 活动编号
     */
    public $activity_id = null;

    /** 
     * 下单会员
     */
    public $member_id = null;

    /** 
     * 拼团失败会员
     */
    public $fail_member_id = null;

    /** 
     * 收货地址
     */
    public $address_id = null;

    /** 
     * 支付通道(0为支付宝,1为微信,2为余额,默认0)
     */
    public $pay_type = null;

    /** 
     * 价格
     */
    public $amount = null;

    /** 
     * 邮费
     */
    public $fee = null;

    /** 
     * 状态(0待支付,1为待发货,2为已发货,3为确认收货,18下单失败,当type为1时,该字段默认值为1)
     */
    public $stats = null;

    /** 
     * 退货退款状态(0为不退款不退货,1为发起退款,2为发起退货,3为退款成功,4为退货成功,5为退款失败,6为退货失败（卖家拒绝）,7为退货等待买家发货,8为退货买家已发货,9退货失败（卖家拒收）默认为0,当type为1时,该字段固定值0.当stats为1时方可发起退款,当stats为3时方可发起退货)
     */
    public $refund_stats = null;

    /** 
     * 评价状态(0为买家未评价,1为买家已评价,默认0,当stats为3时方可修改为1,买家评价后变为2)
     */
    public $rate_stats = null;

    /** 
     * 运费
     */
    public $post_fee = null;

    /** 
     * 成本
     */
    public $cost = null;

    /** 
     * 退款退货服务费
     */
    public $service_fee = null;

    /** 
     * 异步回调时间
     */
    public $notified_at = null;

    /** 
     * 发起退款时间
     */
    public $refunded_at = null;

    /** 
     * 发起退货时间
     */
    public $returned_at = null;

    /** 
     * 下单时间
     */
    public $created_at = null;

    /** 
     * 开始拼团时间
     */
    public $coupon_at = null;

    /** 
     * 更新时间
     */
    public $updated_at = null;
    /** 
     * DAO对象
     */
    public $obj = '';

    /** 
     * DAO对象数组
     */
    public $data = [];

    /** 
     * 翻页对象
     */
    public $meta = null;

    /** 
     * 当前页
     */
    public $page = 1;

    /** 
     * 每页显示条数
     */
    public $page_size = 10;

    /** 
     * 主键数组
     */
    public $primary_key_arr = ["sn"];

    /** 
     * 自增字段数组
     */
    public $auto_increment_key_arr = [];

    /** 
     * 唯一字段
     */
    public $unique_key_arr = '';

    /** 
     * 单例实例
     */
    static public $instance = null;

    /**
     * @return OrderModel|null
     * @throws ModelSqlException
     */
    static public function getInstance() {
        if(is_null(self::$instance)){
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * OrderModel constructor.
     * @throws ModelSqlException
     */
    public function __construct() {
        parent::__construct(str_replace("Model", "", substr(get_class($this), strrpos(get_class($this), "\\")+1)));
        $this->meta = new \stdClass();
    }

    /**
     * @throws ModelException
     */
    public function __clone() {
        throw new ModelException(
            str_replace('%s', get_class($this), ModelException::INSTANCE_NOT_ALLOW_TO_CLONE), ModelException::INSTANCE_NOT_ALLOW_TO_CLONE_NO);
    }

    public function __destruct() {
        self::$instance = null;
    }

    /**
     * set属性
     *
     * __set. 
     * @param string $name 参数名
     * @param string $value 参数值
     * @return $this
     */
    public function __set($name = "", $value = "") {
        $this->$name = $value;
        return $this;
    }

    /**
     * get属性
     *
     * __get. 
     * @param string $name 参数名
     * @return mixed
     */
    public function __get($name = "") {
        return  $this->$name ;
    }

    /**
     * 创建orderModel
     * 
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ModelException
     */
    public function insert() {
        return $this->insertRecord();
    }

    /**
     * 编辑orderModel
     * 
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ModelException
     */
    public function update() {
        return $this->updateRecord();
    }

    /**
     * 删除orderModel，需先指定主键
     * 
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ModelException
     */
    public function delete() {
        return $this->deleteRecord();
    }

    /**
     * 根据主键查询orderModel，获取单条记录
     *
     * find. 
     * @param int $primary_key 主键编号
     * @return $this
     * @throws ModelException
     * @throws ModelReflectionException
     */
    public function find($primary_key = 0) {
        $result = $this->findByPrimaryKey($primary_key);

        $this->setModelProperty($result);

        return $this;
    }

    /**
     * 根据条件数组查询orderModel，获取多条记录
     *
     * findOneBy. 
     * @param array $condition 查询条件
     * @return $this
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     */
    public function findOneBy($condition = []) {
        $result = $this->findRecordBy($condition);

        $this->setModelProperty($result);

        return $this;
    }

    /**
     * 根据条件数组查询orderModel，获取多条记录
     *
     * findBy. 
     * @param array $condition 查询条件
     * @return $this
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     */
    public function findBy($condition = []) {
        $count = $this->getCount($condition);
        $total = $count->fetchColumn();

        $this->data = [];
        $orde_obj = $this->findRecordBy($condition, $this->page, $this->page_size);

        $this->genMeta($total);
        $this->setModelProperty($orde_obj, true);

        return $this;
    }

    /**
     * 批量插入orderModel
     * 
     * @return mixed
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelSqlException
     */
    public function batchInsert() {
        return $this->batchInsertRecord();
    }

    /**
     * 批量删除orderModel
     * 
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelSqlException
     */
    public function batchDelete() {
        $this->batchDeleteRecord();
    }

    /**
     * 生成批量生成update的sql
     * 
     * @return bool|string
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelSqlException
     */
    public function genBatchUpdateSql() {
        return $this->_genBatchUpdateSql();
    }

}